﻿<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Untitled Page" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register TagPrefix="Club" TagName="WebContent" Src="~/UserControls/WebContent.ascx" %>

<%@ Register TagPrefix="Club" TagName="LoginBanner" Src="~/UserControls/LoginBanner.ascx" %>
<%@ Register TagPrefix="Club" TagName="ImageThumbnail" Src="~/UserControls/ImageThumbnail.ascx" %>
<script runat="server">
    Private imageFileLocation As ImageUtils.ImageLocation

    Function privatelabel(ByVal isprivate As Boolean) As String
        If isprivate Then
            Return "Album is private"
        Else
            Return "Album is not private"
        End If
    End Function
    
    Protected Sub DataList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        pn.SelectedPage = DataList2.SelectedIndex + 1
        FormView1.ChangeMode(FormViewMode.ReadOnly)
        FormView2.ChangeMode(FormViewMode.ReadOnly)
    End Sub

    Protected Sub UploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If FileUpload1.HasFile = True And IsAdmin Then
            Dim imageid As Integer
            Dim albumid As Integer
            
            Dim o As Object = Request.QueryString("AlbumID")
            If Not o Is Nothing Then
                albumid = CInt(o)
            End If
            
            If imageFileLocation = ImageUtils.ImageLocation.Database Then
                imageid = ImageUtils.uploadImage(imgTitle.Text, albumid, FileUpload1.FileContent)
            ElseIf imageFileLocation = ImageUtils.ImageLocation.FileSystem Then
                imageid = ImageUtils.uploadImage(imgTitle.Text, albumid, FileUpload1.FileContent, Server.MapPath("~\Pictures") & "\")
            End If

            
            DataList2.DataBind()
            DataList2.SelectedIndex = DataList2.Items.Count - 1
            
            FormView2.DataBind()
            FormView2.PageIndex = FormView2.PageCount - 1
            ErrorLabel.Text = ""
        Else
            ErrorLabel.Text = "<p>Please select a file to upload<p>"
        End If
    End Sub
    
    Private IsAdmin As Boolean
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        IsAdmin = User.IsInRole("Administrators")
        uploadpanel.Visible = IsAdmin
        deletealbum.Visible = IsAdmin
        DetermineStorageLocation()
    End Sub

    Protected Sub deletealbum_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ClubStarterKit.Web.Photos.DeleteAlbum(CInt(Request("albumid")))
        Response.Redirect("~/photo/list.aspx")
    End Sub
    
    Private Sub DetermineStorageLocation()
        imageFileLocation = ImageUtils.GetLocation()
    End Sub

    Protected Sub SelectImage_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs)
        Dim pages As Integer = ClubStarterKit.Web.Photos.NumPages(CInt(Request("albumid")))
        pn.Count = pages
    End Sub

    Protected Sub pn_SelectedPageChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        DataList2.SelectedIndex = pn.SelectedPage - 1
        FormView1.ChangeMode(FormViewMode.ReadOnly)
    End Sub

    Protected Sub SelectImage_Deleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs)
        Response.Redirect("~/photo/contents.aspx?albumid=" & Request.QueryString("albumid"))
    End Sub

    Protected Sub BulkUploadLink_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("~/photo/bulkupload.aspx?albumid=" & Request.QueryString("albumid"))
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="body">
        <Club:LoginBanner ID="LoginBanner1" runat="server" />
        <div id="columnleft">
            <a name="content_start" id="content_start"></a>
            <div class="leftblock">
                <Club:WebContent ID="WebContent1" runat="server" Section="photos" />
                <asp:ScriptManager id="ScriptManager1" runat="server">
                </asp:ScriptManager>
            </div>
            <asp:ObjectDataSource ID="ImagesList" runat="server" SelectMethod="LoadImages" TypeName="ClubStarterKit.Web.Photos" OldValuesParameterFormatString="original_{0}">
                <SelectParameters>
                    <asp:QueryStringParameter Name="albumid" QueryStringField="albumid" Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>
            <asp:ObjectDataSource ID="SelectImage" runat="server" SelectMethod="LoadImage" DeleteMethod="DeleteImage" TypeName="ClubStarterKit.Web.Photos" UpdateMethod="UpdateAlbum" OnSelecting="SelectImage_Selecting" OldValuesParameterFormatString="{0}" OnDeleted="SelectImage_Deleted">
                <UpdateParameters>
                    <asp:Parameter Name="title" Type="String" />
                    <asp:Parameter Name="notes" Type="String" />
                    <asp:Parameter Name="id" Type="int32" />
                </UpdateParameters>
                <SelectParameters>
                    <asp:QueryStringParameter Name="albumid" QueryStringField="albumid" Type="Int32" />
                    <asp:ControlParameter Name="pageindex" ControlID="pn" PropertyName="SelectedPage" />
                </SelectParameters>
                <DeleteParameters>
                    <asp:Parameter Name="id" Type="int32" />
                </DeleteParameters>
            </asp:ObjectDataSource>
            <asp:ObjectDataSource ID="AlbumDetails" runat="server" SelectMethod="SelectAlbumDetails" TypeName="ClubStarterKit.Web.Photos" UpdateMethod="UpdateAlbum" OldValuesParameterFormatString="{0}">
                    <UpdateParameters>
                        <asp:Parameter Name="title" Type="String" />
                        <asp:Parameter Name="isprivate" Type="Boolean" />
                        <asp:Parameter Name="description" Type="String" />
                        <asp:QueryStringParameter Name="albumid" Type="int32" QueryStringField="albumid" />
                    </UpdateParameters>
                    <SelectParameters>
                        <asp:QueryStringParameter Name="albumid" QueryStringField="albumid" Type="Int32" />
                    </SelectParameters>
            </asp:ObjectDataSource>
            
           <div class="leftblock">
                <h2>
                    Album Contents</h2>
                <asp:UpdatePanel id="UpdatePanel2" runat="server">
                    <contenttemplate>
                        <asp:DataList ID="DataList2" runat="server" DataSourceID="ImagesList" DataKeyField="id"
                            RepeatColumns="2" CellSpacing="2" SelectedIndex="0" ItemStyle-CssClass="unselected"
                            SelectedItemStyle-CssClass="selected" OnSelectedIndexChanged="DataList2_SelectedIndexChanged">
                            <ItemTemplate>
                                <asp:ImageButton ID="ImageButton1" ImageUrl='<%# "~/UserControls/imagefetch.ashx?size=1&imageid=" & Cstr(Eval("id")) %>'
                                    runat="server" CommandName="select" /><br />
                                <asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" />
                            </ItemTemplate>
                            <SelectedItemStyle CssClass="selected" />
                            <ItemStyle CssClass="unselected" />
                        </asp:DataList>
                    </contenttemplate>
                </asp:UpdatePanel><br />
                <asp:UpdateProgress id="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel2"
                    DisplayAfter="5">
                    <progresstemplate>
                        <asp:Image ID="loadImage" runat="server" SkinID="loading" />
                    </progresstemplate>
                </asp:UpdateProgress>
            </div>
        </div>
        <div id="columnright">
            <div class="rightblock">
                <asp:UpdatePanel ID="AlbumDetailsPanel" runat="server">
                    <ContentTemplate>
                    <asp:FormView ID="FormView1" runat="server" DataSourceID="AlbumDetails" Width="100%"
                    DataKeyNames="AlbumID">
                    <ItemTemplate>
                        <h2>
                            Album
                            <asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" />
                        </h2>
                        <p>
                            <asp:Label ID="privateLabel" runat="server" Text='<%# PrivateLabel(CBool(Eval("isprivate"))) %>' />
                        </p>
                        <asp:Label ID="desclabel" runat="server" Text='<%# Eval("description") %>' />
                        <div class="actionbuttons">
                            <Club:RolloverButton ID="GreenRolloverButton2" CommandName="Edit" Text="Edit Album"
                                runat="server" Visible='<%#IsAdmin %>' />
                        </div>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <table>
                            <tr>
                                <td class="formlabel">
                                    Album Name</td>
                                <td>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("title") %>' CssClass="txtfield" /></td>
                            </tr>
                            <tr>
                                <td>
                                </td>
                                <td>
                                    <asp:CheckBox ID="CheckBox1" runat="server" Text="Album is private and non visible to other users"
                                        Checked='<%#Bind("isprivate") %>' /></td>
                            </tr>
                            <tr>
                                <td class="formlabel">
                                    Notes</td>
                                <td>
                                    <asp:TextBox ID="notesedit" runat="server" Text='<%# Bind("description") %>' TextMode="MultiLine"
                                        Rows="5" CssClass="txtblock" /></td>
                            </tr>
                        </table>
                        <div class="actionbuttons">
                            <Club:RolloverButton ID="update" CommandName="Update" Text="Apply" runat="server" />
                            <Club:RolloverButton ID="cancel" CommandName="Cancel" Text="Cancel" runat="server" />
                        </div>
                    </EditItemTemplate>
                </asp:FormView>
                    </ContentTemplate>
                </asp:UpdatePanel>
                <asp:UpdateProgress ID="AlbumDetailsPanelload" runat="server" AssociatedUpdatePanelID="AlbumDetailsPanel" DisplayAfter="200">
                    <ProgressTemplate>
                        <asp:Image ID="AlbumDetailsIMG" runat="server" SkinID="loading" />
                    </ProgressTemplate>
                </asp:UpdateProgress>
                <div class="actionbuttons">
                <Club:RolloverButton ID="deletealbum" runat="server" OnClick="deletealbum_Click" Text="Delete Album" />
                </div>
            </div>
            <div class="rightblock">
                &nbsp;<asp:UpdatePanel id="UpdatePanel1" runat="server"><contenttemplate>
                <asp:FormView ID="FormView2" runat="server" DataSourceID="SelectImage" DataKeyNames="id"
                    Width="445px" PagerSettings-Visible="false">
                    <ItemTemplate>
                        <h2>
                            Selected Photo:
                            <asp:Label Text='<%# Eval("title") %>' runat="server" ID="descriptionLabel" />
                        </h2>
                        <a href='<%# "../UserControls/imagefetch.ashx?size=2&ImageID=" & CSTR(Eval("id")) %>' target="_blank">
                        <Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("id") %>' /></a>
                        <p>
                            <asp:Label ID="noteslabel" runat="server" Text='<%#Eval("notes") %>' />
                        </p>
                        <p>
                            <asp:HyperLink ID="downloadimg" runat="server" Text="Download full size image" NavigateUrl='<%# "~/UserControls/imagefetch.ashx?size=2&ImageID=" & CSTR(Eval("id")) %>'
                                Target="_blank" />
                        </p>
                        <asp:Panel ID="editbtns1" runat="server" CssClass="actionbuttons" Visible='<%# IsAdmin %>'>
                            <Club:RolloverButton ID="delete" CommandName="Delete" Text="Delete Photo" runat="server" />
                            <Club:RolloverButton ID="edit" CommandName="Edit" Text="Edit Photo"
                                runat="server" />
                        </asp:Panel>
                        <div class="dashedline">
                        </div>
                        <div>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <h2>
                            Edit Photo Properties</h2>
                        <Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("id") %>' />
                        <table>
                            <tr>
                                <td class="formlabel">
                                    Photo Caption:
                                </td>
                                <td>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%#Bind("title") %>' CssClass="txtfield" /></td>
                            </tr>
                            <tr>
                                <td class="formlabel">
                                    Notes</td>
                                <td>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%#Bind("notes") %>' Rows="3" TextMode="MultiLine"
                                        CssClass="txtblock" /></td>
                            </tr>
                        </table>
                        <div class="actionbuttons">
                            <Club:RolloverButton ID="update" CommandName="Update" Text="Apply" runat="server" />
                            <Club:RolloverButton ID="cancel" CommandName="Cancel" Text="Cancel" runat="server" />
                        </div>
                    </EditItemTemplate>
                    <PagerSettings Visible="False" />
                </asp:FormView>
                    <Club:PhotoNumberer ID="pn" runat="server" SelectedPage="1" CssClass="PageNumbers" OnSelectedPageChanged="pn_SelectedPageChanged" />
                </contenttemplate>
                </asp:UpdatePanel>
                <asp:UpdateProgress id="UpdateProgress2" runat="server" AssociatedUpdatePanelID="UpdatePanel1"><progresstemplate>
<asp:Image ID="Image2" runat="server" SkinID="loading" />
</progresstemplate>
                </asp:UpdateProgress>
            </div>
            <asp:Panel ID="uploadpanel" runat="server" CssClass="rightblock">
                <h2>
                    Upload Photo</h2>
                <asp:Label ID="ErrorLabel" runat="server" Text="" />
                <table>
                    <tr>
                        <td class="formlabel">
                            File:</td>
                        <td>
                            <asp:FileUpload ID="FileUpload1" runat="server" CssClass="txtfield" /></td>
                    </tr>
                    <tr>
                        <td class="formlabel">
                            Image title:</td>
                        <td>
                            <asp:TextBox ID="imgTitle" runat="server" CssClass="txtfield" />
                        </td>
                    </tr>
                </table>
                <div class="actionbuttons">
                    <Club:RolloverButton ID="UploadFile" runat="server" Text="Upload" OnClick="UploadFile_Click" />
                    <br /><br />
                    <Club:RolloverButton ID="BulkUploadLink" runat="server" Text="Bull Upload" OnClick="BulkUploadLink_Click" />
                </div>
            </asp:Panel>

            
            <br />
            &nbsp;
        </div>
        <div class="clear2column">
        </div>
    </div>
</asp:Content>
